package cn.wizzer.app.web.modules.controllers.user;


import cn.wizzer.app.user.modules.models.User;
import cn.wizzer.app.user.modules.services.GroupInfoService;
import cn.wizzer.app.user.modules.services.UserService;
import cn.wizzer.app.web.commons.filter.ApiTokenFilter;
import cn.wizzer.framework.base.model.enums.BizExceptionEnum;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.annotation.*;

/**
 * 客服
 *
 * @author .
 */
@IocBean
@At("/api/user/attendant")
public class ApiAttendantController {
    @Reference
    @Inject
    private GroupInfoService groupInfoService;

    @Reference
    @Inject
    private UserService userService;

    /**
     * 客服列表
     *
     * @param keyword 搜索关键字
     * @return
     */
    @At("/list")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object list(@ParamVerify(name = "keyword") String keyword) {
        String appid = Mvcs.getReq().getHeader("appid");
        Cnd cnd = Cnd.where("id", "!=", appid).and("attendant", "=", 1);
        // 查询用户列表
        if (Strings.isNotBlank(keyword)) {
            keyword = "%" + keyword + "%";
            cnd.and(Cnd.exps("nickName", "LIKE", keyword)
                    .or("userName", "LIKE", keyword)
            );
        }
        return Result.success().addData(userService.query("^(id|headPicture|nickName|userName|isOnline|attendant)$", cnd));
    }


    /**
     * 用户找在线客服
     *
     * @return
     */
    @At("/online")
    @Ok("json:full")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object online() {
        String appid = Mvcs.getReq().getHeader("appid");
        User user = userService.fetch(appid);
        // 判断是不是临时账号
        if (appid.contains("TEMPID")) {
            // 查询数据库如果有，就不用创建，如果没有就要创建
            if (user == null) {
                user = userService.attendTemp(appid, Lang.getIP(Mvcs.getReq()));
            }
        }
        if (user.isAttendant()) {
            return Result.error(BizExceptionEnum.ATTENDANT_PLEASE_LOGIN_PC);
        }
        return Result.success().addData(groupInfoService.onlineAttendant(appid));
    }


    /**
     * 移交其他客服
     *
     * @param groupId     私聊群id
     * @param attendantId 要移交的客服userId
     * @return
     */
    @At("/transfer")
    @Ok("json")
    @Filters(@By(type = ApiTokenFilter.class))
    public Object transfer(String groupId, String attendantId) {
        // 当前客服userId
        String appid = Mvcs.getReq().getHeader("appid");
        if (appid.equals(attendantId)) {
            return Result.error(BizExceptionEnum.ALREADY_IN_THE_GROUP);
        }
        groupInfoService.transferAttendant(groupId, appid, attendantId);
        return Result.success();
    }

}
